package org.ruoyi.aibox.service.impl;

import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.core.page.TableDataInfo;
import org.ruoyi.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.aibox.domain.bo.DocAuditResultDetailBo;
import org.ruoyi.aibox.domain.vo.DocAuditResultDetailVo;
import org.ruoyi.aibox.domain.DocAuditResultDetail;
import org.ruoyi.aibox.mapper.DocAuditResultDetailMapper;
import org.ruoyi.aibox.service.IDocAuditResultDetailService;
import org.ruoyi.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文档审核结果明细Service业务层处理
 *
 * @author xpeter
 * @date 2025-04-29
 */
@RequiredArgsConstructor
@Service
public class DocAuditResultDetailServiceImpl implements IDocAuditResultDetailService {

    private final DocAuditResultDetailMapper baseMapper;

    /**
     * 查询文档审核结果明细
     */
    @Override
    public DocAuditResultDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询文档审核结果明细列表
     */
    @Override
    public TableDataInfo<DocAuditResultDetailVo> queryPageList(DocAuditResultDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DocAuditResultDetail> lqw = buildQueryWrapper(bo);
        Page<DocAuditResultDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文档审核结果明细列表
     */
    @Override
    public List<DocAuditResultDetailVo> queryList(DocAuditResultDetailBo bo) {
        LambdaQueryWrapper<DocAuditResultDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DocAuditResultDetail> buildQueryWrapper(DocAuditResultDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DocAuditResultDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getResultId() != null, DocAuditResultDetail::getResultId, bo.getResultId());
        lqw.eq(bo.getAuditItemId() != null, DocAuditResultDetail::getAuditItemId, bo.getAuditItemId());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditItemResult()), DocAuditResultDetail::getAuditItemResult, bo.getAuditItemResult());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditAdvice()), DocAuditResultDetail::getAuditAdvice, bo.getAuditAdvice());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditHumanResult()), DocAuditResultDetail::getAuditHumanResult, bo.getAuditHumanResult());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditHumanAdvice()), DocAuditResultDetail::getAuditHumanAdvice, bo.getAuditHumanAdvice());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditUser()), DocAuditResultDetail::getAuditUser, bo.getAuditUser());
        return lqw;
    }

    /**
     * 新增文档审核结果明细
     */
    @Override
    public Boolean insertByBo(DocAuditResultDetailBo bo) {
        DocAuditResultDetail add = MapstructUtils.convert(bo, DocAuditResultDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改文档审核结果明细
     */
    @Override
    public Boolean updateByBo(DocAuditResultDetailBo bo) {
        DocAuditResultDetail update = MapstructUtils.convert(bo, DocAuditResultDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DocAuditResultDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文档审核结果明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
